-module(troop).

-include("records.hrl").
-include_lib("eunit/include/eunit.hrl").

-export([new/2, create/2, get_attack/1, get_defense/1]).

new(Branch, Amount) -> #troop{branch = Branch, amount = Amount}.

create(Branch, Amount) -> 
	Troop = new(Branch, Amount),
	active_record:create(Troop#troop{id = rand:new()}).

get_attack(Troop) -> Troop#troop.amount * (get_branch(Troop))#branch.attack.

get_defense(Troop) -> Troop#troop.amount * (get_branch(Troop))#branch.defense.

get_branch(Troop) -> Troop#troop.branch.

%% Tests

creation_test_() ->
	Branch = branch:new("Infantry", 4, 3),
	Troop = new(Branch, 10),
	[
	?_assertMatch(10, Troop#troop.amount),
	?_assertMatch(40, get_attack(Troop)),
	?_assertMatch(30, get_defense(Troop))
	].
